﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace LaoLuo.Webform
{
    /// <summary>
    /// author: zhaojie
    /// </summary>
    /// <typeparam name="T"></typeparam>
    internal class ViewManager<T> where T : UserControl
    {
        private Page _pageHolder;

        public T LoadViewControl(string path)
        {
            this._pageHolder = new Page();
            return (T)this._pageHolder.LoadControl(path);
        }

        public string RenderView(T control)
        {
            var output = new StringWriter();

            this._pageHolder.Controls.Add(control);
            HttpContext.Current.Server.Execute(this._pageHolder, output, false);

            return output.ToString();
        }
    }

    public static class ViewManager
    {
        public static T LoadViewControl<T>(string path)
            where T : UserControl
        {
            return (T)new Page().LoadControl(path);
        }

        public static string Render<T>(this T control)
            where T : UserControl
        {
            var output = new StringWriter();
            var page = new Page();
            page.Controls.Add(control);
            HttpContext.Current.Server.Execute(page, output, false);
            return output.ToString();
        }
    }
}
